Project Description¶
We are working on a registration system for a mobile app at a small startup. The product managers have asked us to improve the current sign-up process by integrating reusable Python functions that check and validate the user input entered into the registration form.
Our main task is to use these validation functions to approve or reject sign-up attempts before creating user accounts. By combining the existing validation functions with the new registration functions we will develop, we aim to make the sign-up process more reliable and improve the experience for thousands of app users.
A previous junior developer has already written some helper functions that validate user names, emails, and passwords. These functions are saved in a file named python_functions.py
. They will help us register users, store their information safely, and handle any errors that happen during sign-up.
Before using these helper functions, we need to understand how they work. We will carefully read the docstrings for each of the functions: validate_name
, validate_email
, and validate_password
. This will help us use them correctly and build a better registration system.
# examine the docstring of each function
from python_functions import validate_name, validate_email, validate_password, top_level_domains
print("validate_name\n")
print(validate_name.__doc__)
print("--------------------\n")
print("validate_email\n")
print(validate_email.__doc__)
print("--------------------\n")
print("validate_password\n")
print(validate_password.__doc__)
# The top level domains variable is used in validate_email to approve only certain email domains
print(top_level_domains)
validate_name Checks that the name is greater than two characters and is a string data type. Args: name (str): The inputted name from the user. Returns: bool: True if the name passes the check, False otherwise. -------------------- validate_email Checks that the email address is in a valid format, has a username greater than 1 character, an '@' symbol, and an allowed domain that is in the `top_level_domains` variable. Args: email (str): The inputted email from the user. Returns: bool: True if the email passes the checks, False otherwise. -------------------- validate_password Checks that the password is strong enough. It should include a capital letter, a number between 0-9 and be greater than 8 characters. Args: password (str): The inputted password from the user. Returns: bool: True if the password passes the checks, False otherwise. ['.org', '.net', '.edu', '.ac', '.uk', '.com']
Create a validate_user() function, using some helper validation functions to verify user input¶
def validate_user(name, email, password):
"""Validate the user name, email and password.
Args:
name (string): Name that we're attempting to validate.
email (string): Email address that we're attempting to validate.
password (string): Password that we're attempting to validate.
Returns:
bool: Returns True if all validation checks pass.
Raises:
ValueError: If any validation check fails.
"""
if validate_name(name) == False:
raise ValueError("Please make sure your name is greater than 2 characters!")
if validate_email(email) == False:
raise ValueError("Your email address is in the incorrect format, please enter a valid email.")
if validate_password(password) == False:
raise ValueError("Your password is too weak, ensure that your password is greater than 8 characters, "
"contains a capital letter and a number.")
return True
Create a register_user() function to handle the registration logic¶
def register_user(name, email, password):
"""Attempt to register the user if they pass validation.
Args:
name (string): Name of the user.
email (string): Email address of the user.
password (string): Password of the user.
Returns:
dict or bool: Returns a dictionary with the user details if validation is successful,
or False if the validation fails.
"""
try:
validate_user(name, email, password)
except:
return False
user = {
"name": name,
"email": email,
"password": password
}
return user